#include "ConsoleLogger.h"
#include <stdint.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>

const static uint16_t LOG_COLOR[LOG_LEVEL_FATAL + 1] = {
	0,
	0,
	FOREGROUND_BLUE | FOREGROUND_GREEN,
	FOREGROUND_GREEN | FOREGROUND_RED,
	FOREGROUND_RED,
	FOREGROUND_GREEN,
	FOREGROUND_RED | FOREGROUND_BLUE };
#else
const static char LOG_COLOR[LOG_LEVEL_FATAL + 1][50] = {
	"\e[0m",
	"\e[0m",
	"\e[34m\e[1m",//hight blue
	"\e[33m", //yellow
	"\e[31m", //red
	"\e[32m", //green
	"\e[35m" };
#endif

ConsoleLogger::ConsoleLogger()
{
}


ConsoleLogger::~ConsoleLogger()
{
}

void ConsoleLogger::DoPrint(LogItem & li)
{
	showColorText(li.buffer, li.lv);
}

void ConsoleLogger::showColorText(const char *text, LOG_LEVEL lv)
{
	if (lv <= LOG_LEVEL_DEBUG || lv > LOG_LEVEL_FATAL)
	{
		printf("%s", text);
		return;
	}
#ifndef WIN32
	printf("%s%s\e[0m", LOG_COLOR[lv], text);
#else
	HANDLE hStd = ::GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStd == INVALID_HANDLE_VALUE) return;
	CONSOLE_SCREEN_BUFFER_INFO oldInfo;
	if (!GetConsoleScreenBufferInfo(hStd, &oldInfo))
	{
		return;
	}
	else
	{
		SetConsoleTextAttribute(hStd, LOG_COLOR[lv]);
		printf("%s", text);
		SetConsoleTextAttribute(hStd, oldInfo.wAttributes);
	}
#endif
	return;
}
